/**
 * 
 */
package com.attilax.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.attilax.net.urlEncode;
import com.attilax.net.websitex;

/**
 * @author ASIMO
 *
 */
public class UrlX {

	/**
	 * @author attilax 老哇的爪子
	 * @throws IOException
	 * @since obb h_46_s
	 */
	public static void main(String[] args) throws IOException {
		// String
		// downloadUrl="http://211.147.235.199:8888/vod/uploadf/huangweicheng/music2.mp4";
		// downloadUrl="http://192.168.1.33/vod/uploadf/01.mp4";
		// int fileSize = getFileSize(downloadUrl);
		// System.out.println(fileSize);
		System.out.println(URLEncoder.encode("全国邮编区号搜索", "gbk"));

		String file = "http://192.168.1.33/vod/uploadf/1205.rmvb";
		String t_urlencode = URLEncoder.encode(file, "utf-8");
		String url = "http://localhost/vod/api.jsp?submethod=getFileSize&appid=1234507y767676&sign=xxxxxx&param="
				+ t_urlencode;
		System.out.println(websitex.WebpageContent(url));
		;

	}

	private static Map<String, String> getHeader(String url) {
		Map<String, String> map = new TreeMap<String, String>();
		int start = url.indexOf("?");
		if (start >= 0) {
			String str = url.substring(start + 1);
			System.out.println(str);
			String[] paramsArr = str.split("&");
			for (String param : paramsArr) {
				String[] temp = param.split("=");
				map.put(temp[0], temp[1]);
			}
		}
		return map;
	}

	public static String getQueryStr(Map map, String enc) {
		// Map<String, String> map = new TreeMap<String, String>();
		// int start = url.indexOf("?");
		// if (start >= 0) {
		// String str = url.substring(start + 1);
		// System.out.println(str);
		// String[] paramsArr = str.split("&");
		// for (String param : paramsArr) {
		// String[] temp = param.split("=");
		// map.put(temp[0], temp[1]);
		// }
		// }
		String s = "";
		Set<Map.Entry<String, Object>> set = map.entrySet();
		for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it
				.hasNext();) {
			Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
					.next();
			if (s.equals(""))
				try {
					s = entry.getKey()
							+ "="
							+ URLEncoder.encode(entry.getValue().toString(),
									enc);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			else
				try {
					s = s
							+ "&"
							+ entry.getKey()
							+ "="
							+ URLEncoder.encode(entry.getValue().toString(),
									enc);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return s;
	}

	public static long getFileSize(String downloadUrl)
			throws MalformedURLException, IOException, ProtocolException {
		URL url = new URL(downloadUrl);

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5 * 1000);
		// conn.setRequestProperty("Accept-Language", Params.ACCEPT_LANGUAGE);
		// conn.setRequestProperty("Charset", Params.ChARSET);
		conn.setRequestMethod("GET");
		int statusCode = conn.getResponseCode();
		if (statusCode != 200)
			throw new RuntimeException("httpstate:"
					+ String.valueOf(statusCode) + " url:" + downloadUrl);
		// if(conn)
		// conn.setRequestProperty("Connetion", "Keep-Alive");
		// 文件大小

		long fileSize = conn.getContentLengthLong();
		return fileSize;
	}

}
